/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author new
 */
public class EquHandler {

    public int getEquTable(String FileName) throws FileNotFoundException, IOException{
        String temp = "EquTemp";
        Pattern patternS;
        Matcher matcherS;
        patternS = Pattern.compile("\\w*(?=\\s{0,65536}\\bEQU\\b)",Pattern.CASE_INSENSITIVE);
        Pattern patternD;
        Matcher matcherD;
        patternD = Pattern.compile("(?<=\\bEQU\\b\\s{0,65536})\\w*",Pattern.CASE_INSENSITIVE);
        FileHandler fh = new FileHandler();
        BufferedReader br = fh.openReadFile(FileName);
        BufferedWriter bw = fh.openWriteFile(temp);
        String tempStr = br.readLine();
        String[] tempStrArray = new String[3];
        equSource = new ArrayList();
        equDest = new ArrayList();
        while(tempStr != null){
            if(tempStr.contains("EQU")){
                matcherS = patternS.matcher(tempStr);
                matcherD = patternD.matcher(tempStr);
                if(matcherS.find()){
                    tempStrArray[0] = matcherS.group();
                } else {
                    System.out.println("EQU error.");
                    System.exit(0);
                }
                if(matcherD.find()){
                    tempStrArray[2] = matcherD.group();
                } else {
                    System.out.println("EQU error.");
                    System.exit(0);
                }
                if(LabelHandler.isConflictWithReverse(tempStrArray[0])){
                    System.out.println(tempStrArray[0] + " is conflict with reverse word.");
                    System.exit(0);
                }
                if(LabelHandler.isConflictWithSegment(tempStrArray[0])){
                    System.out.println(tempStrArray[0] + " is conflict with segment label.");
                    System.exit(0);
                }
                if(LabelHandler.isConfilictWithLabel(tempStrArray[0])){
                    System.out.println(tempStrArray[0] + " is conflict with label.");
                    System.exit(0);
                }
                if(equSource.contains(tempStrArray[0])){
                    System.out.println(tempStrArray[0] + " define twice.");
                    System.exit(0);
                }
                equSource.add("\\b" + tempStrArray[0] + "\\b");
                equDest.add(tempStrArray[2]);
                tempStr = br.readLine();
                continue;
            }
            bw.append(tempStr);
            bw.newLine();
            tempStr = br.readLine();
        }
        fh.closeReadFile(br);
        fh.closeWriteFile(bw);
        System.gc();
        fh.replaceFile(temp, FileName);
        equTableLength = equSource.size();

        return equTableLength;
    }

    public int equReplace(String FileName) throws FileNotFoundException, IOException{
        String temp = "EquTemp.txt";
        FileHandler fh = new FileHandler();
        BufferedReader br;
        BufferedWriter bw;

        while(true){
            int i = 0;

            br = fh.openReadFile(FileName);
            bw = fh.openWriteFile(temp);
            String tempStr = br.readLine();

            while(tempStr != null){
                String tempResult = equReplaceLine(tempStr);
                if(!tempResult.equals(tempStr)){
                    i++;
                }
                bw.append(tempResult);
                bw.newLine();
                tempStr = br.readLine();
            }
            fh.closeReadFile(br);
            fh.closeWriteFile(bw);
            System.gc();
            fh.replaceFile(temp, FileName);
            if(i == 0){
                break;
            }
        }

        return 0;
    }

    private String equReplaceLine(String tempStr) {
        String result = new String(tempStr);
        Pattern pattern;
        Matcher matcher;

        for(int i = 0; i< equTableLength; i++){
            pattern = Pattern.compile((String)equSource.get(i),Pattern.CASE_INSENSITIVE);
            matcher =pattern.matcher(result);
            result = matcher.replaceAll((String)equDest.get(i));
        }
        return result;
    }

    private ArrayList equSource;
    private int equTableLength;
    private ArrayList equDest;
}
